1253B - Silly Mistake - CodeForces Solution


greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin>>n;
  vector<int>nums(n);
  for(int i=0;i<n;i++) cin>>nums[i];
  map<int,int>m1,m2;
  int cnt=0,days=0;
  vector<int>ans;
  for(int i=0;i<n;i++){
    if(nums[i]>0){
      if(m1.find(nums[i])==m1.end()){
        m1[nums[i]]++;
        m2[nums[i]]++;
        cnt++,days++;
      }
      else{
        cout<<"-1\n";
        return 0;
      }
    }
    else{
      if(m2[-1*nums[i]]!=0 && m1.find(-1*nums[i])!=m1.end()){
        m2[-1*nums[i]]--;
        days++;
        cnt--;
      }
      else{
        cout<<"-1\n";
        return 0;
      }
    }
    if(cnt==0 && days>0){
      ans.push_back(days);
      days=0;
      m1.clear();
    }
  }
  if(cnt>0 || ans.size()==0) cout<<"-1\n";
  else{
    cout<<ans.size()<<"\n";
    for(int i=0;i<ans.size();i++) cout<<ans[i]<<" ";
  }
}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations